package com.lgc.common.ktx

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.WindowManager.LayoutParams.*
import android.view.inputmethod.InputMethodManager
import androidx.activity.ComponentActivity
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import com.bumptech.glide.manager.Lifecycle

/**
 * Activity中使用Databinding时setContentView的简化
 * layout:布局文件
 * @return 返回一个binding的对象实例
 */
fun <T:ViewDataBinding> Activity.bindView(@LayoutRes layout:Int):T{
    return DataBindingUtil.setContentView(this,layout)
}

/**
 * View中使用Databinding时setContentView的简化
 */
fun <T:ViewDataBinding> Activity.bindView(view: View):T?{
    return DataBindingUtil.bind<T>(view)
}

/**
 * 界面Activity的沉浸式状态栏,使得可以在状态栏里面显示部分需要的图片
 * 注意点:需要在setContentView之前调用该函数才有效
 */
fun Activity.immediateStatusBar(){
    window.apply {
//        addFlags(windowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
        addFlags(FLAG_LAYOUT_IN_SCREEN)
        addFlags(FLAG_TRANSLUCENT_STATUS)
        addFlags(FLAG_LAYOUT_NO_LIMITS)
        decorView.systemUiVisibility=View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    }
}

/**
 * 软键盘的隐藏
 * view:需要隐藏软键盘的view
 */
fun Activity.dissmissKeyBoard(view:View?){
    val imm:InputMethodManager? = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
    imm?.hideSoftInputFromWindow(view?.windowToken,0)
}


//region 扩展属性

/**
 * 扩展lifecyleOwner属性，便于和fragment之间使用lifeCycleOwner一致
 */
val ComponentActivity.viewLifeCycleOwner:LifecycleOwner
    get() = this

/**
 * Activity的扩展字段,便于和Fragment中使用liveData类的时候，参数一致性
 */
val Activity.context:Context
    get() = this

//endregion